package com.zhiyou.crud.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou.crud.bean.*;
import com.zhiyou.crud.service.UserCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/UserCenter")
public class UserCenterController {

    @Autowired
    UserCenterService userCenterService;

    /*
     * 查询我发起的所有项目
     * */
    @RequestMapping("SelectProject")
    public String getIStartProject(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model, Integer uno) {
        PageHelper.startPage(pn,2); //传入页码和每页显示的数量
        List<Project> iStartProject = userCenterService.getIStartProject(uno);
        PageInfo page=new PageInfo(iStartProject,5); //连续显示的页数
        model.addAttribute("pageInfo",page);
        return "/user1.1";
    }

    /*
     * 查询我发起的所有众筹中的项目
     * */
    @RequestMapping("SelectProject11")
    public String getIStartProject11(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model, Integer uno) {
        PageHelper.startPage(pn,2); //传入页码和每页显示的数量
        List<Project> iStartProject = userCenterService.getIStartProject(uno);
        PageInfo page=new PageInfo(iStartProject,5); //连续显示的页数
        model.addAttribute("pageInfo",page);
        return "/user1.1";
    }

    /*
     * 查询我参与的项目
     * */
    @RequestMapping("SelectProject2")
    public String getIjoinProject2(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model,Integer uno) {
        System.out.println("传过来的："+uno);
        PageHelper.startPage(pn,2); //传入页码和每页显示的数量
        List<Donate> donates = userCenterService.getIjoinProject(uno);
        PageInfo page=new PageInfo(donates,5); //连续显示的页数
        model.addAttribute("pageInfo",page);
        return "/user1.2";
    }

    /*
     * 查询我的捐赠记录
     * */
    @RequestMapping("SelectProject4")
    public String getIjoinProject4(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model,Integer uno) {
        PageHelper.startPage(pn,2); //传入页码和每页显示的数量
        List<Donate> donates = userCenterService.getIjoinProject(uno);
        PageInfo page=new PageInfo(donates,5); //连续显示的页数
        model.addAttribute("pageInfo",page);
        return "/user1.4";
    }

    /*
     * 查询我的评论
     * */
    @RequestMapping("SelectComment")
    public String getMyComment(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model,Integer uno) {
        PageHelper.startPage(pn,2); //传入页码和每页显示的数量
        List<Comment> comments=userCenterService.getMyComment(uno);
        PageInfo page=new PageInfo(comments,5); //连续显示的页数
        model.addAttribute("pageInfo",page);
        return "/user2.1";
    }

    /*
     * 查询我的私信/通知
     * */
    @RequestMapping("SelectInform")
    public String getMyInform(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model,Integer uno) {
        PageHelper.startPage(pn,2); //传入页码和每页显示的数量
        List<Letter> letters=userCenterService.getMyInform(uno);
        PageInfo page=new PageInfo(letters,5); //连续显示的页数
        model.addAttribute("pageInfo",page);
        return "/user2.2";
    }

    /*
     * 查询我的个人资料
     * */
    @RequestMapping("SelectMyData")
    public String getMyData(Integer uno,Model model) {
        User user=userCenterService.getMyData(uno);
        model.addAttribute("user",user);
        return "/user3.1";
    }

    /*
     * 实名认证前查询个人实名状态
     * */
    @RequestMapping("SelectMyStatus")
    public String getMyData2(Integer uno,Model model) {
        System.out.println(uno);
        User user=userCenterService.getMyData(uno);
        model.addAttribute("user",user);
        return "/user3.4";
    }

    /*
     * 提交实名认证，同时修改
     * */
    @RequestMapping("UserAutonym")
    public String userAutonym(Real real,Model model)
    {
        boolean b = userCenterService.userAutonymAndstatus(real);
        if (b){
            System.out.println(real.getStatus());
            model.addAttribute("real", real);
            System.out.println("成功");

            System.out.println();
            return "redirect:/UserCenter/SelectMyStatus?uno="+real.getUno();
        }


        return "redirect:/UserCenter/SelectMyStatus?uno="+real.getUno();
    }

    /*
     * 修改密码/资料修改
     * */
    @RequestMapping("updateUserData")
    public String updateUserPassword(User user) {
        boolean b = userCenterService.updateUserPassword(user);
        if (b){
            System.out.println("更改成功");
        }else {
            System.out.println("更改失败");
        }
        return "redirect:/UserCenter/SelectMyData?uno="+user.getUno();
    }
}
